import java.util.ArrayList;
import java.util.Collections;


public class GeneticVillageSimulator 
{
	public static int iteration = 0;
	public static boolean dontQuit;
	public static ArrayList<LearningEngine> engines;
	public static int num_villages = 5;


	//number of game iterations before villages mix populations
	public static int recombination_threshhold = 10;

	//count of iterations since the last village recombination
	public static int iteration_count = 0;


	public static void recombine()
	{
		System.out.println("Recombining village populations.");

		ArrayList<group1_KalahHeuristic> all_village_generations = new ArrayList<group1_KalahHeuristic>();

		//number of villagers per village
		int village_size = engines.get(0).current_generation.size();

		for(LearningEngine engine : engines)
		{
			all_village_generations.addAll(engine.current_generation);
		}

		//now we have a list of all the villagers, shuffle and redistribute them
		Collections.shuffle(all_village_generations);

		//int total_villagers = all_village_generations.size();

		int current_villager = 0;

		for(int j = 0; j < num_villages; j++)
		{
			ArrayList<group1_KalahHeuristic> new_generation = new ArrayList<group1_KalahHeuristic>();

			for(int k = 0; k < village_size; k++)
			{
				new_generation.add(all_village_generations.get(current_villager));
				current_villager++;
			}

			engines.get(j).current_generation = new_generation;
		}

		System.out.println("Completed village recombination.");
		System.out.println();

		try 
		{
			Thread.sleep(1000);
		} 
		catch (InterruptedException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void main(String args[]) throws Exception 
	{
		iteration = 0;
		dontQuit = true;

		engines = new ArrayList<LearningEngine>();

		for(int i = 0; i < num_villages; i++)
		{
			LearningEngine engine = new LearningEngine(i);
			engines.add(engine);
		}

		if (LearningEngine.initGeneration)
			iteration++;

		new QuitFrame();

		if((args.length > 0) && (args[0].compareTo("superTournament") == 0))
			superTournament();

		while(dontQuit)
		{

			if((iteration%recombination_threshhold) == 0)
			{
				//recombine populations
				recombine();
			}

			for(LearningEngine engine : engines)
			{
				System.out.println("Village "+engine.village_id+" is beginning iteration: " + iteration );
				engine.runGeneticEngine();
				System.out.println();
//				try 
//				{
//					Thread.sleep(10000);
//				} 
//				catch (InterruptedException e) 
//				{
//					e.printStackTrace();
//				}

			}

			iteration++;


		}

		System.out.println("Ending Villiage Simulator");

	}	

	public static void superTournament(){
		LearningEngine superEngine = new LearningEngine(100, false);
		ArrayList<group1_KalahHeuristic> all_village_generations = new ArrayList<group1_KalahHeuristic>();
		
		

		for(LearningEngine engine : engines)
		{	
			for(int i = 0; i < 10; i++){
				all_village_generations.add(engine.current_generation.get(i));
			}
		}
		
		System.out.format("\nRunning the Super Tournament %d Players present.\n", all_village_generations.size());
		
		superEngine.current_generation = all_village_generations;
		
		superEngine.runGeneticEngine();

	}



}
